/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bms.model;

import java.util.ArrayList;
import java.util.List;



/**
 *
 * @author Mhhubhe
 */
public class Pagination {
    //private ArrayList resultProfiles;

    String showmessage;
    Battery battery = new Battery();    
    ArrayList resultProfiles = battery.getlistBatteries();
    private ArrayList<Integer> pagination = new ArrayList<>();
    private ArrayList<Integer> temppagination = new ArrayList<>();
    List tempresultprofiles;
    Boolean previousflag, nextflag, showpage;
    String forward, forwardSearchResultPage = "";
    private int currentpage, start_index, end_index;
    
    //BatteryMeterSystemView viewer = new BatteryMeterSystemView(app);
    
    public Pagination() {
        
        int resultprofilelen = battery.getlistBatteries().size();
        
        if (resultprofilelen != 0 && resultprofilelen <= 20) {
            
            showpage = false;
            this.setShowpage(showpage);
            
            showmessage = "Showing all of " + resultprofilelen + " Results";
            System.out.println(showmessage);
            
            tempresultprofiles = resultProfiles;
            //tempresultprofiles = profileService.getimageid(tempresultprofiles);
        }
        //*/
        if (resultprofilelen > 20) {
            int reminder = resultprofilelen % 20;
            int numberpages = resultprofilelen / 20;
            showpage = true;
            for (int i = 1; i <= numberpages; i++) {
                pagination.add(i);
            }
            if (reminder >= 1) {
                
                pagination.add(pagination.size() + 1);
            }
            
            if (pagination.size() > 10) {
                currentpage = 1;
                page();
                
            } else {
                temppagination = pagination;
            }
            
            currentpage = 1;
            previousflag = false;
            nextflag = true;
            
            
            showmessage = "Showing " + currentpage * 1 + " - " + currentpage * 20 + " of " + resultprofilelen;
            System.out.println(showmessage);
            
            tempresultprofiles = resultProfiles.subList(0, 20);
            this.setEnd_index(20);
            
            this.setShowpage(showpage);
        }
    }
    
    public String page() {
        int startindex = 0;
        int endindex = 20;
        int nopages = pagination.size();
        startindex = (currentpage - 1) * 20 + 1;
        
        this.setStart_index(startindex);
        this.setEnd_index(endindex);
        
        if (currentpage == nopages) {
            endindex = resultProfiles.size();
            this.setEnd_index(endindex);
        }
        if (currentpage < nopages) {
            endindex = currentpage * 20;
            this.setEnd_index(endindex);
        }
        
        
        if (currentpage == nopages) {
            tempresultprofiles = resultProfiles.subList(startindex - 1, resultProfiles.size());
        } else {
            tempresultprofiles = resultProfiles.subList(startindex - 1, endindex);
        }
        
        forward = forwardSearchResultPage;
        showmessage = "Showing " + startindex + " - " + endindex + " of " + resultProfiles.size();
        System.out.println(showmessage);
        
        if (currentpage == 1) {
            previousflag = false;
            nextflag = true;
            
        }
        if (currentpage > 1 && currentpage != nopages) {
            previousflag = true;
            nextflag = true;
        }
        if (currentpage == nopages) {
            previousflag = true;
            nextflag = false;
        }
        
        temppage();
        //tempresultprofiles= profileService.getimageid(tempresultprofiles);
        System.out.println(showpage + "\t <<" + previousflag + " : " + nextflag + ">>");
        return forward;
    }

    public Boolean getShowpage() {
        return showpage;
    }

    public void setShowpage(Boolean showpage) {
        this.showpage = showpage;
    }
    
    
    public void first() {
        currentpage = 1;
        page();
    }
    
    public void previous() {
        currentpage = currentpage - 1;
        page();
    }
    
    public void next() {
        currentpage = currentpage + 1;
        page();
    }
    
    public void last() {
        currentpage = pagination.size();
        page();
    }
    
    public Boolean getPreviousFlag(){
        return this.previousflag;
    }
    
    public Boolean getNextFlag(){
        return this.nextflag;
    }
    

    public int getEnd_index() {
        return end_index;
    }

    public void setEnd_index(int end_index) {
        this.end_index = end_index;
    }

    public int getStart_index() {
        return start_index;
    }

    public void setStart_index(int start_index) {
        this.start_index = start_index;
    }
    public void temppage() {
        temppagination = new ArrayList<Integer>();
        int startindex = 0;
        int endindex = 0;
        if (pagination.size() > 10) {
            if (currentpage > 5) {
                int diff = pagination.size() - currentpage;
                if (diff >= 5) {
                    startindex = currentpage - 4;
                    this.setStart_index(startindex);
                } else {
                    startindex = pagination.size() - 10;
                    this.setStart_index(startindex);
                }
            } else {
                startindex = 0;
                this.setStart_index(startindex);
            }
            
            if (pagination.size() >= startindex + 9) {
                endindex = startindex + 10;
                this.setEnd_index(endindex);
            } else {
                endindex = pagination.size();
                this.setEnd_index(endindex);
            }
            
            
            for (int i = startindex; i < endindex; i++) {
                temppagination.add(i + 1);
            }
            
        } else {
            temppagination = pagination;
        }
    }
    
    public static void main(String[] args){
        Pagination pager = new Pagination();
        
        //pager.next();
        System.out.println("subList(" + pager.getStart_index() + "," + pager.getEnd_index() + " : " + pager.getShowpage() + ") \n");
        
        if (pager.getShowpage()){
        pager.next();
        System.out.println("subList(" + pager.getStart_index() + "," + pager.getEnd_index() + ") \n");
        }
        
        /*
        pager.previous();
        System.out.println("subList(" + pager.getStart_index() + "," + pager.getEnd_index() + ") \n");
        
        pager.next();
        System.out.println("subList(" + pager.getStart_index() + "," + pager.getEnd_index() + ") \n");
        
        //pager.next();
        
        //pager.previous();
        
        //pager.next();
        
        //pager.next();
        */
    }
}
